import time
import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By
import win32com.client

speaker = win32com.client.Dispatch("SAPI.SpVoice")

####淘宝购物Python全自动实现####
# 秒杀时间
# kill_time = "2023-09-18 16:00:00.000000"
kill_time = input(f"请输入你要秒杀商品的时间(YYYY-MM-DD HH:MM:SS) : ")
print(f"秒杀时间是 : {kill_time}")
kill_time = "{}{}".format(kill_time, ".000000")
browser = webdriver.Chrome()
browser.get("https://www.taobao.com")
time.sleep(3)
browser.find_element(By.LINK_TEXT, "亲，请登录").click()
print(f"请尽快扫码登录")
time.sleep(15)
browser.get("https://cart.taobao.com/cart.htm")
while True:
    # 当前时间
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
    print(current_time)
    if current_time > kill_time:
        # 购物车 商品全选
        while True:
            current_time = time.strftime("%H:%M:%S", time.localtime())
            print(current_time, end="\r")
            if browser.find_element(By.ID, "J_SelectAll1"):
                browser.find_element(By.ID, "J_SelectAll1").click()
                break
        break
time.sleep(4)
# Python全自化结算
while True:
    try:
        browser.find_element(By.LINK_TEXT, "结 算").click()
        print(f"主人,结算提交成功,我已帮全买到商品啦,请及时支付订单")
        speaker.Speak(f"主人,结算提交成功,我已帮买到商品啦,请及时支付订单")
        time.sleep(1)
        break
    except:
        pass
# 点击提交订单按钮
while True:
    try:
        if browser.find_element(By.LINK_TEXT, '提交订单').click():
            print(f"抢购成功，请尽快付款")
    except:
        print(f"主人,我已帮你抢到商品啦,您来支付吧")
        break
time.sleep(0.01)
